package com.hzsd.zengbanyi.biaodan_text.bean;



import java.lang.reflect.WildcardType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class WorkDAO {
	private Context context;

	public WorkDAO(Context context) {
		this.context = context;
	}
	
	/**
	 * 查询t_mon_sample_hydrology 排污口/现场水文 相关字段
	 * @param sampleId
	 * @param key
	 * @return
	 */
	public String getStandard(String sampleId,String hydrology_name) {
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select hydrology_value from t_mon_sample_hydrology where sample_id = ? and hydrology_name = ?", new String[]{sampleId,hydrology_name});
		String Standard = null;
		while (cursor.moveToNext()) {
			Standard = cursor.getString(0);
			
		}
		cursor.close();
		db.close();
		return Standard;
	}
	/**
	 * 修改t_mon_sample_hydrology 排污口/现场水文 相关字段
	 * @param sampleId
	 * @param key
	 * @param value
	 */
	public void updataStandard(String sampleId,String hydrology_name,String hydrology_value,String type) {
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getWritableDatabase();
		
		String hydrology_id = gethydrologyID(hydrology_name,type);
		String sys_auto_time = getSysAutoTime("t_mon_sample_hydrology_type","hydrology_id",hydrology_id)==null?"":getSysAutoTime("t_mon_sample_hydrology_type","hydrology_id",hydrology_id);
		if (StandardisExist(sampleId,hydrology_id)) {//有此数据，修改
			ContentValues values = new ContentValues();
			values.put("hydrology_value", hydrology_value);
			db.update("t_mon_sample_hydrology", values, "sample_id = ? and hydrology_id = ?", new String[]{sampleId,hydrology_id});
		}else{//无此数据，添加
			db.execSQL("insert into t_mon_sample_hydrology(sample_id,hydrology_id,hydrology_name,hydrology_value,sys_auto_time)values(?,?,?,?,?)", new String[]{sampleId,hydrology_id,hydrology_name,hydrology_value,sys_auto_time});
			
		}
		
		db.close();
	}
	/**
	 * 根据传入值 查询t_mon_sample_hydrology_type 相应的ID
	 * @param hydrology_name
	 * @param hydrology_type
	 * @return
	 */
	public String gethydrologyID(String hydrology_name,String hydrology_type) {
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select hydrology_id from t_mon_sample_hydrology_type where hydrology_name = ? and hydrology_type = ?", new String[]{hydrology_name,hydrology_type});
		String hydrology_id = null;
		while (cursor.moveToNext()) {
			hydrology_id = cursor.getString(0);
			
		}
		cursor.close();
		db.close();
		return hydrology_id;
	}
	/**
	 * 查询t_mon_sample_hydrology 表中是否有此条数据
	 * 
	 * @param sampleId
	 * @param hydrology_id
	 * @return
	 */
	public boolean StandardisExist(String sampleId,String hydrology_id) {
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select * from t_mon_sample_hydrology where sample_id = ? and hydrology_id = ?", new String[]{sampleId,hydrology_id});
		if (cursor.getCount() > 0) {
			return true;
		}
		cursor.close();
		db.close();
		return false;
	}
	
	/**
	 * 查询t_mon_task_scene_extend表  extend_value 字段
	 * @param taskId
	 * @param key   动态传入要查询的字段
	 * @return
	 */
	public String getTaskSceneMoreField(String taskSceneId,String extend_name,String type) {
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		
		String extend_id = getExtendID(extend_name,type);
		
		Cursor cursor = db.rawQuery("select extend_value from t_mon_task_scene_extend where extend_id = ? and task_scene_id = ?", new String[]{extend_id,taskSceneId});
		String text = "";
		while (cursor.moveToNext()) {
			text = cursor.getString(0);
			
		}
		cursor.close();
		db.close();
		return text;
	}
	/**
	 * 修改t_mon_task_scene_extend表
	 */
	public void updateTaskSceneMoreField(String taskSceneId,String text,String extend_name,String type) {
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getWritableDatabase();
		
		String extend_id = getExtendID(extend_name,type);
		String sys_auto_time = getSysAutoTime("t_mon_task_scene_extend_type","extend_id",extend_id)==null?"":getSysAutoTime("t_mon_task_scene_extend_type","extend_id",extend_id);
		if ("".equals(extend_id)) {
			db.close();
			return;
		}
		if (t_mon_task_scene_extendisExist(taskSceneId,extend_id)) {
			ContentValues values = new ContentValues();
			values.put("extend_value", text);
			db.update("t_mon_task_scene_extend", values, "extend_id = ? and task_scene_id = ?", new String[]{extend_id,taskSceneId});
			
		}else{
			db.execSQL("insert into t_mon_task_scene_extend(task_scene_id,extend_id,extend_name,extend_value,sys_auto_time)values(?,?,?,?,?)",new String[]{taskSceneId,extend_id,extend_name,text,sys_auto_time});
		}
		db.close();
		
	}
	/**
	 * 获取对应时间--sys_auto_time
	 * @param extend_id
	 * @return
	 */
	public String getSysAutoTime(String table,String where,String extend_id) {
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select sys_auto_time from "+table+" where "+where+" = ?", new String[]{extend_id});
		String sys_auto_time = null;
		if (cursor.moveToFirst()) {
			sys_auto_time = cursor.getString(cursor.getColumnIndex("sys_auto_time"));
		}
		cursor.close();
		db.close();
		return sys_auto_time;
	}
	/**
	 * 判断是否存在这条数据
	 * @param taskSceneId
	 * @param extend_id
	 * @return
	 */
	public boolean t_mon_task_scene_extendisExist(String taskSceneId,String extend_id) {
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select * from t_mon_task_scene_extend where task_scene_id = ? and extend_id = ?", new String[]{taskSceneId,extend_id});
		if (cursor.getCount() > 0) {
			return true;
		}
		return false;
	}
	
	/**
	 * 修改t_mon_task_scene_extend表extend_value(固定剂加入情况 ：|1|2|)字段
	 * @param str
	 * @param taskid
	 */
	public void addFixingAgent(String taskSceneId,String str,String extendName,String type) {
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getWritableDatabase();
		
		String extend_id = getExtendID(extendName,type);
		String sys_auto_time = getSysAutoTime("t_mon_task_scene_extend_type","extend_id",extend_id)==null?"":getSysAutoTime("t_mon_task_scene_extend_type","extend_id",extend_id);
		if (!t_mon_task_scene_extendisExist(taskSceneId,extend_id)) {
			db.execSQL("insert into t_mon_task_scene_extend(task_scene_id,extend_id,extend_name,extend_value,sys_auto_time)values(?,?,?,?,?)",new String[]{taskSceneId,extend_id,extendName,"|"+str+"|",sys_auto_time});
			db.close();
			return;
		}
		
		String fixingAgent = getTaskSceneMoreField(taskSceneId,extendName,type);
		String[] fixingAgents = fixingAgent.split("\\|");
		boolean flag = false;
		for (String string : fixingAgents) {
			if (str.equals(string)) {
				flag = true;
			}
		}
		if (!flag) {//不存在，则添加选中值
			StringBuffer sb = new StringBuffer();
			sb.append(fixingAgent);
			
			ContentValues values = new ContentValues();
			if ("".equals(fixingAgent)) {
				sb.append("|"+str+"|");
			}else{
				sb.append(str+"|");
			}
			values.put("extend_value", sb.toString());
			db.update("t_mon_task_scene_extend", values, "extend_id = ?", new String[]{getExtendID(extendName,type)});
			
		}else{//存在,删除值
			ContentValues values = new ContentValues();
			String fixing = fixingAgent.replace(str+"|", "");
			values.put("extend_value", fixing);
			db.update("t_mon_task_scene_extend", values, "extend_id = ?", new String[]{getExtendID(extendName,type)});
			
		}
		db.close();
		
	}
	
	public String getExtendID(String extendName,String type) {
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select extend_id from t_mon_task_scene_extend_type where extend_name = ? and type = ?", new String[]{extendName,type});
		String extend_id = "";
		while (cursor.moveToNext()) {
			extend_id = cursor.getString(0);
			
		}
		return extend_id;
	}

	/**
	 * 查询sample_end_time,sys_auto_time字段，添加到Sample中
	 * 
	 * @param sample
	 */
	public void getSampletime(Sample sample) {
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db
				.rawQuery(
						"select sample_end_time,sys_auto_time from t_mon_sample where sample_id=?",
						new String[] { sample.sampleId });
		String sample_end_time = null, sys_auto_time = null;
		if (cursor.moveToFirst()) {
			sample_end_time = cursor.getString(cursor
					.getColumnIndex("sample_end_time"));
			sys_auto_time = cursor.getString(cursor
					.getColumnIndex("sys_auto_time"));
		}
		sample.sampleEndTime = sample_end_time;
		sample.sys_auto_time = sys_auto_time;
		cursor.close();
		db.close();
	}

	/**
	 * 查询t_mon_sample表中sampleEndTime，sys_auto_time两个字段 返回Sample
	 * 
	 * @param sample
	 * @return
	 */
	public Sample SelectSample(Sample sample) {
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db
				.rawQuery(
						"select sample_end_time,sys_auto_time from t_mon_sample where sample_id=?",
						new String[] { sample.sampleId });
		if (cursor.moveToFirst()) {
			sample.sampleEndTime = cursor.getString(cursor
					.getColumnIndex("sample_end_time"));
			sample.sys_auto_time = cursor.getString(cursor
					.getColumnIndex("sys_auto_time"));

		}
		cursor.close();
		db.close();
		return sample;
	}

	/**
	 * 痕迹功能： 1.添加样品信息(样品信息只含水的)
	 * 
	 * @param sample
	 */
	public void addSampleMark(Sample sample) {
		if (selectSampleMark(sample)) {
			WorkBaseHelper helper = new WorkBaseHelper(context);
			SQLiteDatabase db = helper.getWritableDatabase();
			db.execSQL(
					"insert into hzst_sample_records(task_scene_id,sample_id,sample_start_time,sample_end_time,sample_state,sample_face,sample_volume,sample_volume_unit,temperature,oxygen,tmd,create_time,sys_auto_time)values(?,?,?,?,?,?,?,?,?,?,?,?,?)",
					new String[] { sample.taskSceneId, sample.sampleId,
							sample.sampleStartTime, sample.sampleEndTime,
							sample.sampleState, sample.sampleFace,
							sample.sampleVolume, sample.sampleVolumeUnit,
							sample.temperature, sample.oxygen, sample.tmd,
							sample.createtime, sample.sys_auto_time });
			db.close();
		}
	}

	/**
	 * 查询痕迹表（hzst_sample_records）中最新记录进行匹配是否不同(返回true:不同)
	 * 
	 * @return
	 */
	public boolean selectSampleMark(Sample sample) {
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db
				.rawQuery(
						"select sample_start_time,sample_state,sample_face,temperature,oxygen,tmd from hzst_sample_records where sample_id=? order by create_time desc",
						new String[] { sample.sampleId });
		if (cursor.getCount() == 0) {
			return true;
		}
		String sample_start_item = null;
		String sample_state = null;
		String sample_face = null;
		String temperature = null;
		String oxygen = null;
		String tmd = null;
		if (cursor.moveToFirst()) {
			sample_start_item = cursor.getString(cursor
					.getColumnIndex("sample_start_time"));
			sample_state = cursor.getString(cursor
					.getColumnIndex("sample_state"));
			sample_face = cursor
					.getString(cursor.getColumnIndex("sample_face"));
			temperature = cursor
					.getString(cursor.getColumnIndex("temperature"));
			oxygen = cursor.getString(cursor.getColumnIndex("oxygen"));
			tmd = cursor.getString(cursor.getColumnIndex("tmd"));
		}
		if (!sample.sampleStartTime.equals(sample_start_item)
				|| !sample.sampleState.equals(sample_state)
				|| !sample.sampleFace.equals(sample_face)
				|| !sample.temperature.equals(temperature)
				|| !sample.oxygen.equals(oxygen) || !sample.tmd.equals(tmd)) {
			return true;
		}
		return false;
	}

	/**
	 * 痕迹功能：1.添加采样方式（瞬时...）
	 * 
	 * @param moreBean
	 */
	public void addSceneMore(MoreBean moreBean) {
		// getSceneMore("1","1","1");
		if (getSceneMore(moreBean.taskSceneId, moreBean.taskways,
				moreBean.tasknum) == true) {
			WorkBaseHelper helper = new WorkBaseHelper(context);
			SQLiteDatabase db = helper.getWritableDatabase();
			db.execSQL(
					"insert into hzst_scene_more_records(task_id,task_scene_id,sample_way,ammeter_num,create_time)values(?,?,?,?,?)",
					new String[] { moreBean.taskID, moreBean.taskSceneId,
							moreBean.taskways, moreBean.tasknum,
							moreBean.createtime });
			db.close();
		}
	}

	/**
	 * 查询痕迹表（hzst_scene_more_records）中最新记录进行匹配是否不同(返回true:不同)
	 * 
	 * @param taskSceneId
	 * @return
	 */
	public boolean getSceneMore(String taskSceneId, String taskways,
			String tasknum) {
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db
				.rawQuery(
						"select sample_way,ammeter_num from hzst_scene_more_records where task_scene_id=? order by create_time desc",
						new String[] { taskSceneId });
		if (cursor.getCount() == 0) {
			return true;
		}
		String sample_way = null;
		String ammeter_num = null;
		if (cursor.moveToFirst()) {
			sample_way = cursor.getString(cursor.getColumnIndex("sample_way"));
			ammeter_num = cursor
					.getString(cursor.getColumnIndex("ammeter_num"));
		}
		cursor.close();
		db.close();
		if (!taskways.equals(sample_way) || !tasknum.equals(ammeter_num)) {
			return true;
		}
		return false;
	}

	/**
	 * 获取日志内容
	 * 
	 * @param sampleId
	 * @return
	 */
	public String getSmplePointLog(String sampleId) {
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db.rawQuery(
				"select memo from hzst_T_SamplePoint_Log where sample_id=?",
				new String[] { sampleId });
		String memo = "";
		while (cursor.moveToNext()) {
			memo = cursor.getString(0);

		}
		return memo;
	}

	/**
	 * 查询预定经纬度
	 * 
	 * @param sampleId
	 * @return
	 */
	public ArrayList<SamplePointLog> getsample(String sampleId) {
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db
				.rawQuery(
						"select point_type,point_id,sample_end_time,sys_auto_time from t_mon_sample where sample_id=?",
						new String[] { sampleId });
		String point_type = null, point_id = null, sample_end_time = null, sys_auto_time = null;
		while (cursor.moveToNext()) {
			point_type = cursor.getString(0);
			point_id = cursor.getString(1);
			sample_end_time = cursor.getString(2);
			sys_auto_time = cursor.getString(3);
		}
		ArrayList<SamplePointLog> arrayList = new ArrayList<SamplePointLog>();
		// point_type （1.污染源 2.水 3.空气 4.噪声）
		if (cursor.getCount() > 0) {
			switch (point_type) {

			case "0":
				point_type = "污染源";
				break;
			case "1":
				point_type = "污染源";
				break;
			case "2":
				point_type = "水";
				break;
			case "3":
				point_type = "空气 ";
				break;
			case "4":
				point_type = "噪声";// 暂定
				break;

			default:
				break;
			}
			cursor.close();
			Cursor cursor2 = db
					.rawQuery(
							"select pointName,pointX,pointY,sys_auto_time from t_base_point where oldType=? and pointList like ?",
							new String[] { point_type, "%|" + point_id + "|%" });

			while (cursor2.moveToNext()) {
				SamplePointLog samplePointLog = new SamplePointLog();
				samplePointLog.setPointName(cursor2.getString(0));
				samplePointLog.setPrePointX(cursor2.getString(1));
				samplePointLog.setPrePointY(cursor2.getString(2));
				samplePointLog.setSys_auto_time(cursor2.getString(3));
				samplePointLog.setPointId(point_id);
				samplePointLog.setSample_end_time(sample_end_time);
				samplePointLog.setSys_auto_time(sys_auto_time);
				arrayList.add(samplePointLog);
			}
			cursor2.close();
		}
		db.close();
		return arrayList;
	}

	/**
	 * 添加日志（GPS超出误差范围，需填写日志说明）
	 */
	public boolean addSamplePoint_Log(ArrayList<SamplePointLog> samplePointLogs) {
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getWritableDatabase();
		// 查询表中如果有数据，则修改，无，则添加
		if (selectSamplePointLog(samplePointLogs.get(0).getSample_id())) {
			ContentValues values = new ContentValues();
			values.put("task_id", samplePointLogs.get(0).getTask_id());
			values.put("sample_id", samplePointLogs.get(0).getSample_id());
			values.put("sample_code", samplePointLogs.get(0).getSample_code());
			values.put("pointId", samplePointLogs.get(0).getPointId());
			values.put("pointName", samplePointLogs.get(0).getPointName());
			values.put("prePointX", samplePointLogs.get(0).getPrePointX());
			values.put("prePointY", samplePointLogs.get(0).getPrePointY());
			values.put("realPointX", samplePointLogs.get(0).getRealPointX());
			values.put("realPointY", samplePointLogs.get(0).getRealPointY());
			values.put("memo", samplePointLogs.get(0).getMemo());
			values.put("createTime", samplePointLogs.get(0).getCreateTime());
			values.put("sys_auto_time", samplePointLogs.get(0)
					.getSys_auto_time());
			db.update("hzst_T_SamplePoint_Log", values, "sample_id=?",
					new String[] { samplePointLogs.get(0).getSample_id() });
		} else {
			db.execSQL(
					"insert into hzst_T_SamplePoint_Log(task_id,sample_id,sample_code,pointId,pointName,prePointX,prePointY,realPointX,realPointY,memo,createTime,sys_auto_time)values(?,?,?,?,?,?,?,?,?,?,?,?)",
					new String[] { samplePointLogs.get(0).getTask_id(),
							samplePointLogs.get(0).getSample_id(),
							samplePointLogs.get(0).getSample_code(),
							samplePointLogs.get(0).getPointId(),
							samplePointLogs.get(0).getPointName(),
							samplePointLogs.get(0).getPrePointX(),
							samplePointLogs.get(0).getPrePointY(),
							samplePointLogs.get(0).getRealPointX(),
							samplePointLogs.get(0).getRealPointY(),
							samplePointLogs.get(0).getMemo(),
							samplePointLogs.get(0).getCreateTime(),
							samplePointLogs.get(0).getSys_auto_time() });
		}
		db.close();
		return true;
	}

	/**
	 * 查询hzst_T_SamplePoint_Log表中是否有与sample_id匹配的数据（日志信息） 有数据返回：true 无数据：false;
	 * 
	 * @param sampleId
	 * @return
	 */
	public boolean selectSamplePointLog(String sampleId) {
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db.rawQuery(
				"select * from hzst_T_SamplePoint_Log where sample_id=?",
				new String[] { sampleId });
		if (cursor.getCount() > 0) {
			return true;
		}
		cursor.close();
		db.close();
		return false;

	}

	public ArrayList<Task> getTask(String SceneName,String entity_name) {
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();

		// Cursor cursor =
		// db.rawQuery("SELECT b.task_scene_id,a.monitor_unit_name,b.task_id,b.scene_start_time  FROM t_mon_task_scene_more AS b,(select distinct monitor_unit_name,task_id from v_list_wf_task) AS a WHERE a.task_id = b.task_id and replace(b.scene_man,' ','') like ?",
		// new String[]{SceneName});
		// Cursor cursor =
		// db.rawQuery("SELECT b.task_scene_id,a.monitor_unit_name,b.task_id,b.scene_start_time  FROM t_mon_task_scene_more AS b,(select distinct monitor_unit_name,task_id,user_name from v_list_wf_task) AS a WHERE a.task_id = b.task_id and replace(a.user_name,' ','') or replace(b.scene_man,' ','') like ?",
		// new String[]{SceneName});

		Cursor cursor = db
				.rawQuery(
						"SELECT b.task_scene_id,a.monitor_unit_name,b.task_id,b.scene_start_time,a.task_code  FROM t_mon_task_scene_more AS b,(select distinct monitor_unit_name,task_id,task_code,entity_name from v_list_wf_task) AS a WHERE a.task_id = b.task_id and (replace(b.scene_man,' ','') like ? or b.scene_man = '自送样') and a.entity_name = ?",
						new String[] { SceneName ,entity_name});
		/*if (cursor.getCount() == 0) {
			cursor.close();
			cursor = db
					.rawQuery(
							"SELECT b.task_scene_id,a.monitor_unit_name,b.task_id,b.scene_start_time,a.task_code  FROM t_mon_task_scene_more AS b,(select distinct monitor_unit_name,task_id,task_code from v_list_wf_task) AS a WHERE a.task_id = b.task_id and replace(b.scene_man,' ','') like ?",
							new String[] { SceneName });
		}*/

		ArrayList<Task> listTask = new ArrayList<Task>();
		while (cursor.moveToNext()) {
			Task task = new Task();
			task.taskSceneId = cursor.getString(0);
			task.monitorUnitName = cursor.getString(1);
			task.taskId = cursor.getString(2);
			task.time = cursor.getString(3);
			task.taskCode = cursor.getString(4);
			listTask.add(task);
		}
		cursor.close();
		db.close();
		return listTask;
	}

	/***
	 * 1.此方法备用，上面方法可能有误！ 2.此方法是根据scene_man（采样人员）字段来查的
	 * 3.上面是根据user_name配合scene_man来查的
	 * 
	 * @param SceneName
	 * @return
	 */
	public ArrayList<Task> getTask2(String SceneName) {
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();

		// Cursor cursor =
		// db.rawQuery("SELECT b.task_scene_id,a.monitor_unit_name,b.task_id,b.scene_start_time  FROM t_mon_task_scene_more AS b,(select distinct monitor_unit_name,task_id from v_list_wf_task) AS a WHERE a.task_id = b.task_id and replace(b.scene_man,' ','') like ?",
		// new String[]{SceneName});
		// Cursor cursor =
		// db.rawQuery("SELECT b.task_scene_id,a.monitor_unit_name,b.task_id,b.scene_start_time  FROM t_mon_task_scene_more AS b,(select distinct monitor_unit_name,task_id,user_name from v_list_wf_task) AS a WHERE a.task_id = b.task_id and replace(a.user_name,' ','') or replace(b.scene_man,' ','') like ?",
		// new String[]{SceneName});
		// -----
		// Cursor cursor =
		// db.rawQuery("SELECT b.task_scene_id,a.monitor_unit_name,b.task_id,b.scene_start_time  FROM t_mon_task_scene_more AS b,(select distinct monitor_unit_name,task_id,user_name from v_list_wf_task) AS a WHERE a.task_id = b.task_id and replace(a.user_name,' ','') like ?",
		// new String[]{SceneName});
		// if(cursor.getCount()==0){
		// cursor.close();
		// cursor =
		// db.rawQuery("SELECT b.task_scene_id,a.monitor_unit_name,b.task_id,b.scene_start_time  FROM t_mon_task_scene_more AS b,(select distinct monitor_unit_name,task_id from v_list_wf_task) AS a WHERE a.task_id = b.task_id and replace(b.scene_man,' ','') like ?",
		// new String[]{SceneName});
		// }
		Cursor cursor = db
				.rawQuery(
						"SELECT b.task_scene_id,a.monitor_unit_name,b.task_id,b.scene_start_time  FROM t_mon_task_scene_more AS b,(select distinct monitor_unit_name,task_id from v_list_wf_task) AS a WHERE a.task_id = b.task_id and replace(b.scene_man,' ','') like ?",
						new String[] { SceneName });

		ArrayList<Task> listTask = new ArrayList<Task>();
		while (cursor.moveToNext()) {
			Task task = new Task();
			task.taskSceneId = cursor.getString(0);
			task.monitorUnitName = cursor.getString(1);
			task.taskId = cursor.getString(2);
			task.time = cursor.getString(3);
			listTask.add(task);
		}
		// ======================
		Cursor cursor2 = db
				.rawQuery(
						"select b.task_scene_id,a.monitor_unit_name,b.task_id,b.scene_start_time from t_mon_task_scene_more AS b,(select distinct monitor_unit_name,task_id,user_name from v_list_wf_task) AS a where a.task_id = b.task_id and b.scene_man = '自送样' and replace(a.user_name,' ','') like ?",
						new String[] { SceneName });
		while (cursor2.moveToNext()) {
			Task task = new Task();
			task.taskSceneId = cursor.getString(0);
			task.monitorUnitName = cursor.getString(1);
			task.taskId = cursor.getString(2);
			task.time = cursor.getString(3);
			listTask.add(task);
		}
		// =======================
		cursor.close();
		cursor2.close();
		db.close();
		return listTask;
	}

	/**
	 * 获取sample列表
	 * 
	 * @param taskId
	 *            ,taskSceneId
	 * @return
	 */
	public ArrayList<Sample> getSimple(String taskId, String taskSceneId) {
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db
				.rawQuery(
						"Select sample_general_place,sample_place, sample_id ,sample_code from t_mon_sample where task_id=? and task_scene_id=?",
						new String[] { taskId, taskSceneId });
		ArrayList<Sample> listSample = new ArrayList<Sample>();
		while (cursor.moveToNext()) {
			Sample sample = new Sample();
			sample.sampleGeneralPlace = cursor.getString(0);
			sample.samplePlace = cursor.getString(1);
			sample.sampleId = cursor.getString(2);
			sample.sampleCode = cursor.getString(3);
			listSample.add(sample);
		}
		cursor.close();
		db.close();

		return listSample;

	}

	/***
	 * 根据sampleId查询issave是否保存（保存：issave=1）
	 */
	public boolean getSimpleIsSave(String sampleId) {
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db.rawQuery(
				"Select issave from t_mon_sample where sample_id=?",
				new String[] { sampleId });
		String issave = "";
		while (cursor.moveToNext()) {
			issave = cursor.getString(0);
		}
		cursor.close();
		db.close();
		if ("1".equals(issave)) {
			return true;// 保存了
		}
		return false;// 没保存

	}

	/***
	 * 查询isupload表里是否数据
	 * 
	 * @return
	 */
	public boolean getUploadStateCount() {
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db.rawQuery("Select * from isupload", null);
		if (cursor.getCount() != 0) {
			// 有数据
			cursor.close();
			db.close();
			return true;
		}
		cursor.close();
		db.close();
		return false;

	}

	/***
	 * 添加上传时状态
	 * 
	 * @param taskid
	 * @return
	 */
	public boolean addUploadState(String taskid, String state) {
		boolean issuccess = false;
		if (istaskid(taskid)) {
			// 不存在，直接添加到数据库
			WorkBaseHelper helper = new WorkBaseHelper(context);
			SQLiteDatabase db = helper.getWritableDatabase();
			db.execSQL("insert into isupload (task_id,state)values(?,?)",
					new String[] { taskid, state });
			issuccess = true;
			db.close();
		} else {
			WorkBaseHelper helper = new WorkBaseHelper(context);
			SQLiteDatabase db = helper.getWritableDatabase();
			ContentValues values = new ContentValues();
			values.put("task_id", taskid);
			values.put("state", state);
			db.update("isupload", values, "task_id=?", new String[] { taskid });
			issuccess = true;
			db.close();
		}
		return issuccess;

	}

	/***
	 * 根据taskid查询出状态
	 * 
	 * @param taskid
	 * @return
	 */
	public String getUploadState(String taskid) {
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db.rawQuery(
				"Select state from isupload where task_id=?",
				new String[] { taskid });

		if (cursor.getCount() > 0) {
			String state = "";
			while (cursor.moveToNext()) {
				state = cursor.getString(0);

			}
			// 有上传（包含有的上传有的没有上传）
			if ("0".equals(state)) {
				cursor.close();
				db.close();
				return "0";
			} else {
				cursor.close();
				db.close();
				return "1";
			}
		} else {
			// 从未上传过
			cursor.close();
			db.close();
			return "-1";
		}

	}

	/***
	 * 判断上传的taskid在表里是否存在
	 * 
	 * @param taskid
	 * @return
	 */
	public boolean istaskid(String taskid) {
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db.rawQuery("Select * from isupload where task_id=?",
				new String[] { taskid });
		if (cursor.getCount() == 0) {
			cursor.close();
			db.close();
			return true;
		}
		cursor.close();
		db.close();
		return false;

	}

	/***
	 * 根据taskId查询issave有没有等于1的（保存的）
	 * 
	 * @param taskId
	 * @return
	 */
	public boolean getIsUpload(String taskId) {
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db.rawQuery(
				"Select * from t_mon_sample where issave=? and task_id=?",
				new String[] { "1", taskId });

		if (cursor.getCount() != 0) {
			cursor.close();
			db.close();
			return true;// 上传
		}
		cursor.close();
		db.close();
		return false;// 未上传

	}

	/***
	 * 根据taskid查询已上传中是否有未上传的
	 * 
	 * @param taskid
	 * @return
	 */
	public boolean getNoComplete(String taskid) {
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db.rawQuery(
				"Select * from t_mon_sample where issave=? and task_id=?",
				new String[] { "", taskid });
		if (cursor.getCount() != 0) {
			cursor.close();
			db.close();
			return true;
		}
		cursor.close();
		db.close();
		return false;
	}

	/***
	 * 查询保存了的taskId(在这里是上传成功的taskId)
	 * 
	 * @return
	 */
	public ArrayList<HashMap<String, String>> getArrayListtaskId() {
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db.rawQuery(
				"Select distinct task_id from t_mon_sample where issave=?",
				new String[] { "1" });
		ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
		while (cursor.moveToNext()) {
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("taskId", cursor.getString(0));
			data.add(map);

		}
		cursor.close();
		db.close();
		return data;
	}

	public String getEntityName(String taskId) {
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db
				.rawQuery(
						"Select distinct entity_name from v_list_wf_task where task_id=?",
						new String[] { taskId });
		String entityName = "";
		while (cursor.moveToNext()) {
			entityName = cursor.getString(0);
		}
		cursor.close();
		db.close();

		return entityName;

	}

	public ArrayList<String> GetBaseItem(String taskId, String sampleId) {
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db
				.rawQuery(
						"Select element_name from v_base_item as a,(SELECT item_id from t_mon_sample_item "
								+ "WHERE (sample_id = ?) AND (task_id = ?)) as b where a.item_id=b.item_id",
						new String[] { sampleId, taskId });
		ArrayList<String> baseIdList = new ArrayList<String>();
		while (cursor.moveToNext()) {
			String baseId = cursor.getString(0);
			baseIdList.add(baseId);
		}
		cursor.close();
		db.close();
		return baseIdList;

	}

	public Sample GetSampleDetail(String sampleId) {
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		Sample sample = new Sample();
		Cursor cursor = db
				.rawQuery(
						"Select a.tmd,a.oxygen,a.temperature,a.sample_state,a.sample_face,a.sample_start_time,a.sample_volume,a.sample_volume_unit,b.scene_man,b.record_person from t_mon_sample as a,t_mon_task_scene_more as b"
								+ " where a.task_scene_id = b.task_scene_id and a.sample_id = ?",
						new String[] { sampleId });

		while (cursor.moveToNext()) {
			sample.tmd = cursor.getString(0)==null?"":cursor.getString(0);
			sample.oxygen = cursor.getString(1)==null?"":cursor.getString(1);
			sample.temperature = cursor.getString(2)==null?"":cursor.getString(2);
			sample.sampleState = cursor.getString(3)==null?"":cursor.getString(3);
			sample.sampleFace = cursor.getString(4)==null?"":cursor.getString(4);
			sample.sampleStartTime = cursor.getString(5)==null?"":cursor.getString(5);
			sample.sampleVolume = cursor.getString(6)==null?"":cursor.getString(6);
			sample.sampleVolumeUnit = cursor.getString(7)==null?"":cursor.getString(7);
			sample.sceneMan = cursor.getString(8)==null?"":cursor.getString(8);
			sample.recordPerson = cursor.getString(9)==null?"":cursor.getString(9);
		}
		cursor.close();
		db.close();
		return sample;
	}

	/**
	 * 1.采样方式--查询 2.天气,气温--查询
	 * 
	 * @param taskID
	 * @return
	 */
	public MoreBean getMoreBean(String taskID) {
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		MoreBean moreBean = new MoreBean();
		// Select sample_way,ammeter_num,scene_weather,Temperature from
		// t_mon_task_scene_more where task_id=?
		Cursor cursor = db
				.rawQuery(
						"Select sample_way,ammeter_num,scene_weather,sys_auto_time from t_mon_task_scene_more where task_id=?",
						new String[] { taskID });
		while (cursor.moveToNext()) {
			moreBean.taskways = cursor.getString(0);
			moreBean.tasknum = cursor.getString(1);
			moreBean.scene_weather=cursor.getString(2);
			moreBean.sys_auto_time = cursor.getString(3);
			
		}
		cursor.close();
		db.close();
		return moreBean;
	}

	public boolean updateSample(Sample sample) {
		boolean isSuccess = false;
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getWritableDatabase();
		try {
			ContentValues values = new ContentValues();
			values.put("tmd", sample.tmd);
			values.put("oxygen", sample.oxygen);
			values.put("temperature", sample.temperature);
			values.put("sample_state", sample.sampleState);
			values.put("sample_face", sample.sampleFace);
			values.put("sample_start_time", sample.sampleStartTime);
			values.put("issave", "1");
			db.update("t_mon_sample", values, "sample_id = ?",
					new String[] { sample.sampleId });
			isSuccess = true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			db.close();
		}

		return isSuccess;
	}
	/**
	 * 获取采样时间
	 * @param sampleId
	 * @return
	 */
	public String getSampleStartTime(String sampleId) {
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select sample_start_time,issave from t_mon_sample where sample_id = ?", new String []{sampleId});
		String sampleStartTime = "";
		String issave = "";
		while (cursor.moveToNext()) {
			sampleStartTime = cursor.getString(0);
			issave = cursor.getString(1);
		}
		cursor.close();
		db.close();
		if ("1".equals(issave)) {
			return sampleStartTime;
		}
		return "";
	}
	

	/**
	 * 天气--修改
	 * 
	 * @param moreBean
	 * @return
	 */
	public boolean updateMoreWeather(MoreBean moreBean) {
		boolean isSuccess = false;
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getWritableDatabase();
		try {
			ContentValues values = new ContentValues();
			values.put("scene_weather", moreBean.scene_weather);
			db.update("t_mon_task_scene_more", values, "task_id = ?",
					new String[] { moreBean.taskID });
			isSuccess = true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			db.close();
		}
		return isSuccess;
	}

	

	/**
	 * 采样方式--修改
	 * 
	 * @param moreBean
	 * @return
	 */
	public boolean updateMoreWays(MoreBean moreBean) {
		boolean isSuccess = false;
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getWritableDatabase();
		try {
			ContentValues values = new ContentValues();
			values.put("sample_way", moreBean.taskways);
			db.update("t_mon_task_scene_more", values, "task_id = ?",
					new String[] { moreBean.taskID });
			isSuccess = true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			db.close();
		}
		return isSuccess;
	}

	public boolean updateMoreNum(MoreBean moreBean) {
		boolean isSuccess = false;
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getWritableDatabase();
		try {
			ContentValues values = new ContentValues();
			values.put("ammeter_num", moreBean.tasknum);
			db.update("t_mon_task_scene_more", values, "task_id = ?",
					new String[] { moreBean.taskID });
			isSuccess = true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			db.close();
		}
		return isSuccess;
	}

	public boolean updateGasSample(Sample sample) {
		boolean isSuccess = false;
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getWritableDatabase();
		try {
			ContentValues values = new ContentValues();
			values.put("sample_face", sample.sampleFace);
			values.put("sample_start_time", sample.sampleStartTime);
			values.put("sample_volume", sample.sampleVolume);
			values.put("sample_volume_unit", sample.sampleVolumeUnit);
			values.put("issave", "1");
			db.update("t_mon_sample", values, "sample_id = ?",
					new String[] { sample.sampleId });
			isSuccess = true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			db.close();
		}

		return isSuccess;
	}

	/**
	 * 删除检测项目
	 * 
	 * @param elementName
	 * @param taskId
	 * @param sampleId
	 * @return
	 */
	public boolean deleteBaseItem(String elementName, String taskId,
			String sampleId) {
		boolean isSuccess = false;
		String ItemId = null;
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db.rawQuery(
				"Select item_id from v_base_item  where element_name=?",
				new String[] { elementName });
		while (cursor.moveToNext()) {

			ItemId = cursor.getString(0);
		}
		SQLiteDatabase db1 = helper.getWritableDatabase();
		db1.delete("t_mon_sample_item",
				"item_id=? and  (sample_id =?) and (task_id =?)", new String[] {
						ItemId, sampleId, taskId });

		isSuccess = true;
		cursor.close();
		db.close();
		db1.close();
		return isSuccess;
	}

	public ArrayList<String> GetAllBaseItem() {
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db.rawQuery("Select element_name from v_base_item",
				new String[] {});
		ArrayList<String> baseIdList = new ArrayList<String>();
		while (cursor.moveToNext()) {
			String baseId = cursor.getString(0);
			baseIdList.add(baseId);
		}
		cursor.close();
		db.close();
		return baseIdList;

	}

	/**
	 * 增加检测项目
	 */
	public boolean addBaseItem(String elementName, String taskId,
			String sampleId) {
		boolean isSuccess = false;
		String ItemId = null;
		String sysnumber = null;
		String sample_item_id = null;
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db
				.rawQuery(
						"Select item_id,sysnumber from v_base_item  where element_name=?",
						new String[] { elementName });
		while (cursor.moveToNext()) {
			ItemId = cursor.getString(0);
			sysnumber = cursor.getString(1);

		}
		SQLiteDatabase db1 = helper.getWritableDatabase();
		db1.execSQL(
				"insert into t_mon_sample_item (item_id,sample_id,task_id,sysnumber,sample_item_id)values(?,?,?,?,?)",
				new String[] { ItemId, sampleId, taskId, sysnumber,
						sample_item_id });
		isSuccess = true;
		cursor.close();
		db.close();
		db1.close();
		return isSuccess;
	}

	/**
	 * 添加多媒体path进数据库 type=1 图片 type=2 视频
	 */
	public boolean addMediaPath(String media_path, String task_scene_id,
			int type) {
		boolean isSuccess = false;
		WorkBaseHelper helper = new WorkBaseHelper(context);
		// MediaBaseHelper helper = new MediaBaseHelper(context);
		SQLiteDatabase db = helper.getWritableDatabase();
		String typeString = Integer.toString(type);
		db.execSQL(
				"insert into media (media_path,task_scene_id,media_type)values(?,?,?)",
				new String[] { media_path, task_scene_id, typeString });
		isSuccess = true;
		db.close();
		return isSuccess;
	}

	/**
	 * 获取图片path列表
	 */
	public ArrayList<String> GetPicPath(String task_scene_id) {
		// MediaBaseHelper helper = new MediaBaseHelper(context);
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db
				.rawQuery(
						"Select media_path from media where media_type=1 and task_scene_id=?",
						new String[] { task_scene_id });
		ArrayList<String> pathList = new ArrayList<String>();
		while (cursor.moveToNext()) {
			String path = cursor.getString(0);
			pathList.add(path);
		}
		cursor.close();
		db.close();
		return pathList;

	}

	/**
	 * 获取视频path是否有相同
	 */
	public String GetMvSceneId(String mediaPath) {
		// MediaBaseHelper helper = new MediaBaseHelper(context);
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db
				.rawQuery(
						"Select task_scene_id from media where media_type=2 and media_path=?",
						new String[] { mediaPath });
		String path = "isNotTheSame";
		while (cursor.moveToNext()) {
			path = cursor.getString(0);
		}
		cursor.close();
		db.close();
		return path;

	}

	/**
	 * 获取图片path是否有相同
	 */
	public boolean IsSamePicSceneId(String mediaPath, String taskSceneId) {
		// MediaBaseHelper helper = new MediaBaseHelper(context);
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db
				.rawQuery(
						"Select task_scene_id from media where media_type=1 and media_path=?",
						new String[] { mediaPath });
		String task_scene_id = "isNotTheSame";
		while (cursor.moveToNext()) {
			task_scene_id = cursor.getString(0);
		}
		cursor.close();
		db.close();
		if (task_scene_id.equals(taskSceneId)) {
			return true;
		} else {
			return false;
		}

	}

	/**
	 * 获取视频path列表
	 */
	public ArrayList<String> GetMvPath(String task_scene_id) {
		// MediaBaseHelper helper = new MediaBaseHelper(context);
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db
				.rawQuery(
						"Select media_path from media where media_type=2 and task_scene_id=?",
						new String[] { task_scene_id });
		ArrayList<String> pathList = new ArrayList<String>();
		while (cursor.moveToNext()) {
			String path = cursor.getString(0);
			pathList.add(path);
		}
		cursor.close();
		db.close();
		return pathList;

	}

	/**
	 * 从数据库删除多媒体地址
	 * 
	 * @param media_path
	 * @param task_scene_id
	 * @return
	 */
	public boolean deleteMediaPath(String media_path, String task_scene_id) {
		boolean isSuccess = false;
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db1 = helper.getWritableDatabase();
		db1.delete("media", "media_path=? and  (task_scene_id =?)",
				new String[] { media_path, task_scene_id });
		isSuccess = true;
		db1.close();
		return isSuccess;
	}

	/**
	 * 添加签名path进数据库
	 */
	public boolean addSignaturePath(String signature_path, String task_scene_id) {
		boolean isSuccess = false;
		if (IsSameSignatureSceneIdTeskid(task_scene_id)) {
			WorkBaseHelper helper = new WorkBaseHelper(context);
			SQLiteDatabase db = helper.getWritableDatabase();
			ContentValues values = new ContentValues();
			values.put("signature_path", signature_path);
			db.update("signature", values, "task_scene_id=?",
					new String[] { task_scene_id });
			db.close();
			isSuccess = true;
		} else {
			WorkBaseHelper helper = new WorkBaseHelper(context);
			SQLiteDatabase db = helper.getWritableDatabase();
			// if (IsSameSignatureSceneId(signature_path)) {
			// deleteSignaturePath(signature_path, task_scene_id);
			// db.execSQL("insert into signature (task_scene_id,signature_path)values(?,?)",
			// new String[]{task_scene_id,signature_path});
			// }else{
			db.execSQL(
					"insert into signature (task_scene_id,signature_path)values(?,?)",
					new String[] { task_scene_id, signature_path });
			db.close();
			isSuccess = true;
			// }

		}
		return isSuccess;
	}

	/**
	 * 添加图片签名path进数据库
	 */
	public boolean addSignatureImgPath(String signature_path,
			String task_scene_id) {
		boolean isSuccess = false;

		if (IsSameSignatureSceneIdTeskid(task_scene_id)) {
			WorkBaseHelper helper = new WorkBaseHelper(context);
			SQLiteDatabase db = helper.getWritableDatabase();
			ContentValues values = new ContentValues();
			values.put("signature_imgpath", signature_path);
			db.update("signature", values, "task_scene_id=?",
					new String[] { task_scene_id });
			db.close();
			isSuccess = true;
		} else {
			WorkBaseHelper helper = new WorkBaseHelper(context);
			SQLiteDatabase db = helper.getWritableDatabase();
			// if (IsSameSignatureSceneIdImg(signature_path)) {
			// deleteSignatureImgPath(signature_path, task_scene_id);
			db.execSQL(
					"insert into signature (task_scene_id,signature_imgpath)values(?,?)",
					new String[] { task_scene_id, signature_path });
			// }else{
			// db.execSQL("insert into signature (task_scene_id,signature_imgpath)values(?,?)",
			// new String[]{task_scene_id,signature_path});

			db.close();
			isSuccess = true;
		}

		return isSuccess;
	}

	/**
	 * 获取签名path是否有相同
	 */
	public boolean IsSameSignatureSceneId(String signature_path) {
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db.rawQuery(
				"Select task_scene_id from signature where signature_path=?",
				new String[] { signature_path });
		String path = "isNotTheSame";
		while (cursor.moveToNext()) {
			path = cursor.getString(0);
		}
		cursor.close();
		db.close();
		if (path.equals("isNotTheSame")) {
			return false;
		} else {
			return true;
		}

	}

	/**
	 * 获取图片签名path是否有相同
	 */
	public boolean IsSameSignatureSceneIdImg(String signature_path) {
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db
				.rawQuery(
						"Select task_scene_id from signature where signature_imgpath=?",
						new String[] { signature_path });
		String path = "isNotTheSame";
		while (cursor.moveToNext()) {
			path = cursor.getString(0);
		}
		cursor.close();
		db.close();
		if (path.equals("isNotTheSame")) {
			return false;
		} else {
			return true;
		}

	}

	/**
	 * 获取图片签名taskid是否有相同
	 */
	public boolean IsSameSignatureSceneIdTeskid(String task_scene_id) {
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db.rawQuery(
				"Select task_scene_id from signature where task_scene_id=?",
				new String[] { task_scene_id });
		String taskid = "";
		while (cursor.moveToNext()) {
			taskid = cursor.getString(0);
		}
		cursor.close();
		db.close();
		if (taskid.equals("") || taskid == null) {
			return false;
		} else {
			return true;
		}

	}

	/**
	 * 删除签名path从数据库
	 */
	public boolean deleteSignaturePath(String signature_path,
			String task_scene_id) {
		boolean isSuccess = false;
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db1 = helper.getWritableDatabase();
		db1.delete("signature", "signature_path=? and  (task_scene_id =?)",
				new String[] { signature_path, task_scene_id });
		isSuccess = true;
		db1.close();
		return isSuccess;
	}

	/**
	 * 删除图片签名path从数据库
	 */
	public boolean deleteSignatureImgPath(String signature_path,
			String task_scene_id) {
		boolean isSuccess = false;
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db1 = helper.getWritableDatabase();
		db1.delete("signature", "signature_imgpath=? and  (task_scene_id =?)",
				new String[] { signature_path, task_scene_id });
		isSuccess = true;
		db1.close();
		return isSuccess;
	}

	/**
	 * 获取所有图片视频对象列表
	 */
	public ArrayList<MediaBean> GetAllPicPath() {
		// MediaBaseHelper helper = new MediaBaseHelper(context);
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db.rawQuery(
				"Select media_path,task_scene_id,media_type from media ",
				new String[] {});
		ArrayList<MediaBean> MediaBeanList = new ArrayList<MediaBean>();
		while (cursor.moveToNext()) {
			MediaBean mb = new MediaBean();
			mb.mediaPath = cursor.getString(0);
			mb.taskSenceId = cursor.getString(1);
			mb.type = cursor.getString(2);
			MediaBeanList.add(mb);
		}
		cursor.close();
		db.close();
		return MediaBeanList;

	}

	/**
	 * 获取所有签字对象列表
	 */
	public ArrayList<Signature> GetAllSignaturePath() {
		// MediaBaseHelper helper = new MediaBaseHelper(context);
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db.rawQuery(
				"Select signature_path,task_scene_id from signature ",
				new String[] {});
		ArrayList<Signature> SignatureList = new ArrayList<Signature>();
		while (cursor.moveToNext()) {
			Signature st = new Signature();
			st.signaturePath = cursor.getString(0);
			st.taskSceneId = cursor.getString(1);
			SignatureList.add(st);
		}
		cursor.close();
		db.close();
		return SignatureList;

	}

	/**
	 * 获取图片签字对象列表
	 */
	public ArrayList<Signature> GetAllSignatureImgPath() {
		// MediaBaseHelper helper = new MediaBaseHelper(context);
		WorkBaseHelper helper = new WorkBaseHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db.rawQuery(
				"Select signature_imgpath,task_scene_id from signature ",
				new String[] {});
		ArrayList<Signature> SignatureList = new ArrayList<Signature>();
		while (cursor.moveToNext()) {
			Signature st = new Signature();
			st.signatureImgPath = cursor.getString(0);
			st.taskSceneId = cursor.getString(1);
			SignatureList.add(st);
		}
		cursor.close();
		db.close();
		return SignatureList;

	}
}
